//
//  JCWAlertTextFieldView.swift
//  HengYouCai
//
//  Created by wll on 2017/12/26.
//  Copyright © 2017年 lwj. All rights reserved.
//  有提示文字的输入框 模型高度55

/// margin
private let spaceMargin: CGFloat = 10
///输入框左边距
private let tFLeftViewW: CGFloat = 0
///左边icon大小
private let iconSize: CGSize = CGSize(width: 0, height: 0)

import UIKit

class JCWAlertTextFieldView: UIView {

    ///占位文字
    var placeHolderString: String?
    ///提示文字
    var alertString: String?
    ///图片名字
    var iconImageNamed: String?
    ///右边红线超出距离 eg： 图片验证码
    private var rightViewW:CGFloat?
    

    //MARK:构造函数
    
    ///占位文字 提示文字 图片名字
    convenience init(_ placeHolderString: String?,_ alertString: String?, _ iconImageNamed: String?) {
        self.init(frame: CGRect.zero)
        self.placeHolderString = placeHolderString
        self.iconImageNamed = iconImageNamed
        self.alertString = alertString
        self.addUI()
    }
    
    ///占位文字 提示文字 图片名字 红线超出距离
    convenience init(_ placeHolderString: String?,_ alertString: String?, _ iconImageNamed: String?,_ rightViewW: CGFloat?) {
        self.init(frame: CGRect.zero)
        self.placeHolderString = placeHolderString
        self.iconImageNamed = iconImageNamed
        self.alertString = alertString
        self.rightViewW = rightViewW
        self.addUI()
    }
    

    //MARK:添加UI
    func addUI() {
        self.addSubview(self.alertLab)
        self.addSubview(self.tF)
        self.addSubview(self.sepLineView)
    }
    
    //MARK:布局UI
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.alertLab.frame = CGRect(x: tFLeftViewW, y: spaceMargin+self.alertLab.height_sd, width: self.alertLab.width_sd, height: self.alertLab.height_sd)

        
        self.tF.frame = CGRect(x: 0, y: spaceMargin+self.alertLab.height_sd, width: self.width_sd, height: self.height_sd-self.alertLab.height_sd-spaceMargin)
        
        self.tFLeftView.height_sd = self.tF.height_sd
        
        self.tFLeftView.viewWithTag(10)?.centerY_sd = self.tFLeftView.centerY_sd
        
        self.sepLineView.frame = CGRect(x: 0, y: self.tF.bottom_sd-0.8, width: self.width_sd+(self.rightViewW ?? 0), height: 0.8)
    }
    
    //MARK:懒加载
    
    ///图片
    public lazy var tFLeftView: UIView = {
        let temp = UIView()
        temp.bounds = CGRect(x: 0, y: 0, width: tFLeftViewW, height: 0)
        let subImV = UIImageView()
        subImV.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: iconSize)
        subImV.tag = 10
        subImV.isUserInteractionEnabled = true
        temp.addSubview(subImV)
        guard let _ = self.iconImageNamed else {return temp}
        subImV.image = UIImage(named: self.iconImageNamed!)
        return temp
    }()
    
    ///输入框
    public lazy var tF: UITextField = {
        let temp = UITextField()
        temp.leftViewMode = .always
        temp.leftView = self.tFLeftView
        temp.clearButtonMode = .whileEditing
        temp.textColor = UIColor.white
        temp.font = Font_WL(16)
        temp.tintColor = UIColor.white
        temp.delegate = self
        temp.placeholder = self.placeHolderString
        temp.placeHolderColor = UIColor.white
        return temp
    }()
    
    ///提示
    public lazy var alertLab: UILabel = {
        let temp = UILabel(frame: CGRect.zero)
        temp.text = self.alertString
        temp.textColor = UIColor.white
        temp.font = Font_WL(13)
        temp.isHidden = true
        temp.sizeToFit()
        return temp
    }()
    
    ///华丽的分割线
    public lazy var sepLineView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        return temp
    }()

}


///MARK:___textFieldDelegate___ 处理提示和分割线颜色
extension JCWAlertTextFieldView: UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.alertLab.textColor = UIColor.white
    }
    
    //编辑
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        //显示状态
        if self.alertLab.isHidden == false {
            //字符被清空
            if let str = textField.text,str.count == 1,string.isEmpty {
                self.hideAlertLab()
                return true
            }
            //删除但未清空
            return true
        }
        self.showAlertLab()
        return true
    }
    //清空
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        self.hideAlertLab()
        return true
    }
    //结束编辑
    func textFieldDidEndEditing(_ textField: UITextField) {
        if let str = textField.text, str.isEmpty, self.alertLab.isHidden == false{
            self.hideAlertLab()
        }else{
//            //外部赋值调用结束编辑代理 不显示提示文字
//            if  let str = textField.text, str.isEmpty, self.alertLab.isHidden == true{
//                self.showAlertLab()
//            }
            self.alertLab.textColor = UIColor.white
        }
    }
    
    ///隐藏提示文字
    private func hideAlertLab() {
        UIView.animate(withDuration: 0.1, animations: {
            self.alertLab.top_sd = self.tF.top_sd + self.tF.height_sd/2.0 - self.alertLab.height_sd/2.0
            self.alertLab.alpha = 0
        }, completion: { (completion) in
            self.alertLab.isHidden = true
        })
        self.sepLineView.backgroundColor = UIColor.white
    }
    
    ///显示提示文字
    private func showAlertLab(){
        self.alertLab.isHidden = false
        UIView.animate(withDuration: 0.1, animations: {
            self.alertLab.top_sd = spaceMargin
            self.sepLineView.backgroundColor = UIColor.white
            self.alertLab.alpha = 1
        }, completion: nil)
    }
}
